﻿<%@ WebHandler Language="C#" Class="PZIssueErpMaterialReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 集件配套查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.4.27
 **/

public class PZIssueErpMaterialReport : IHttpHandler
{
    public bool IsReusable { get { return false; } }
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT cci.productname partname,cci.productdescription partdescription,cci.containername,pbn.productname,p.description,pml.qtyrequired,
       NVL(iem.issueqty,0) issueqty,iem.productchildno,e.fullname,iem.createdate
FROM productmateriallistitem pml
LEFT JOIN containercurrentinfo cci ON cci.bomid = pml.bomid
LEFT JOIN productbase pb ON pb.Productbaseid = pml.Productbaseid
LEFT JOIN product p ON p.productid = NVL(pb.revofrcdid,pml.Productid)
LEFT JOIN productbase pbn ON pbn.productbaseid = p.productbaseid
LEFT JOIN issueerpmaterial iem ON iem.bommateriallistitemid = pml.productmateriallistitemid AND cci.containerid = iem.containerid
LEFT JOIN employee e ON iem.employeeid = e.employeeid
WHERE 1=1
";
        //分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //组件图号
        if (jObject["zjproductname"] != null && jObject["zjproductname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["zjproductname"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND pbn.productname LIKE '%{0}%'", jObject["productname"].ToString());
        }

        //组件批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername LIKE '%{0}%'", jObject["containername"].ToString());
        }

        //配套人
        if (jObject["peitaoren"] != null && jObject["peitaoren"].ToString() != "")
        {
            sql += string.Format(" AND e.fullname LIKE '%{0}%'", jObject["peitaoren"].ToString());
        }

        //配套时间
        if (jObject["createdate"] != null && jObject["createdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createdate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND TRUNC(iem.createdate) BETWEEN TO_DATE('{0}','yyyy-mm-dd') and TO_DATE('{1}','yyyy-mm-dd')", arr[0].ToString(), arr[1].ToString());
            }
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();

        string sql = getSearchSqlString(jObject);

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dt = dal.GetDataTable(sql);

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dt.Columns["partname"].Caption = "组件图号";
        dt.Columns["partdescription"].Caption = "组件名称";
        dt.Columns["containername"].Caption = "组件批次";
        dt.Columns["productname"].Caption = "零件图号";
        dt.Columns["description"].Caption = "零件名称";
        dt.Columns["qtyrequired"].Caption = "零件数量";
        dt.Columns["issueqty"].Caption = "集件数量";
        dt.Columns["productchildno"].Caption = "集件批次";
        dt.Columns["fullname"].Caption = "配套人";
        dt.Columns["createdate"].Caption = "配套时间";

        ExportHelper.ExportExcel(dt, "集件配套查询报表");
    }
}